﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Dzzzr.DataLayer;

namespace Dzzzr
{
  public partial class Game 
  {
    public static Game GetGameByID(int gameID)
    {
      Game game = null;
      using (SqlConnection cnn = Helper.Connection)
      {
        SqlCommand cmd = new SqlCommand("GetGameByID", cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@ID", gameID);
        SqlDataReader rd = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        if (rd.Read())
        {
          game = new Game();
          game.ID = rd.GetInt32(0);
          game.TeamID = rd.GetInt32(1);
          game.Name = rd.GetString(2);
        }
      }
      return game;
    }

    public static List<Game> GetGameList(int teamID)
    {
      List<Game> games = new List<Game>();
      using (SqlConnection cnn = Helper.Connection)
      {
        SqlCommand cmd = new SqlCommand("GetGameList",cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@TeamID", teamID);
        SqlDataReader rd = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        while(rd.Read())
        {
          Game game = new Game();
          game.ID = rd.GetInt32(0);
          game.TeamID = rd.GetInt32(1);
          game.Name = rd.GetString(2);
          games.Add(game);
        }
      }
      return games;
    }
    public static int InsertGame(int TeamID, string Name, string Description, DateTime CreationDate, int ID )
    {
      object id = -1;
      using (SqlConnection cnn = Helper.Connection)
      {
        SqlCommand cmd = new SqlCommand("InsertGame", cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 0, ParameterDirection.Output, false, 0, 0, null, DataRowVersion.Current, id));
        cmd.Parameters.AddWithValue("@TeamID", TeamID);
        cmd.Parameters.AddWithValue("@Name", Name);
        cmd.Parameters.AddWithValue("@Description", Description);
        cmd.Parameters.AddWithValue("@CreationDate", CreationDate);
        cmd.ExecuteNonQuery();
      }
      return (int) id;
    }
    public static void DeleteGame(int ID)
    {
      using (SqlConnection cnn = Helper.Connection)
      {
        SqlCommand cmd = new SqlCommand("DeleteGame", cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@ID", ID);
        cmd.ExecuteNonQuery();
      }
    }

    public static void AssignEntityToGame(int gameID, int entityID, bool assigned)
    {
      using (SqlConnection cnn = Helper.Connection)
      {
        SqlCommand cmd = new SqlCommand("AssignEntityToGame", cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@EntityID", entityID);
        cmd.Parameters.AddWithValue("@GameID", gameID);
        cmd.Parameters.AddWithValue("@Assigned", assigned);
        cmd.ExecuteNonQuery();
      }
    }

    public static Game GetGameByName(int teamID, string gameName)
    {
      Game game = null;
      using (SqlConnection cnn = Helper.Connection)
      {
        SqlCommand cmd = new SqlCommand("GetGameByName", cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@TeamID", teamID);
        cmd.Parameters.AddWithValue("@Name", gameName);
        SqlDataReader rd = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        if (rd.Read())
        {
          game = new Game();
          game.ID = rd.GetInt32(0);
          game.TeamID = rd.GetInt32(1);
          game.Name = rd.GetString(2);
        }
      }
      return game;
    }

  }
}
